package com.guiji.job.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guiji.job.vo.UnTagListVo;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.guiji.common.annotation.RepeatSubmit;
import com.guiji.common.annotation.Log;
import com.guiji.common.core.controller.BaseController;
import com.guiji.common.core.domain.AjaxResult;
import com.guiji.common.enums.BusinessType;
import com.guiji.job.vo.JobVo;
import com.guiji.job.bo.JobQueryBo;
import com.guiji.job.bo.JobAddBo;
import com.guiji.job.bo.JobEditBo;
import com.guiji.job.service.IJobService;
import com.guiji.common.utils.poi.ExcelUtil;
import com.guiji.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 任务主Controller
 *
 * @author 钟维澎（四月）
 * @date 2021-07-02
 */
@Api(value = "任务主控制器", tags = {"任务主管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/job/job")
public class JobController extends BaseController {

    private final IJobService iJobService;

    /**
     * 查询任务主列表
     */
    @ApiOperation("查询待打标任务主列表")
    @PreAuthorize("@ss.hasPermi('job:job:list')")
    @GetMapping("/pending/list")
    public TableDataInfo<UnTagListVo> PendingList(@Validated JobQueryBo bo) {
		return iJobService.queryPendingList(bo);
    }

	/**
	 * 查询任务主列表
	 */
	@ApiOperation("查询任务主列表")
	@PreAuthorize("@ss.hasPermi('job:job:list')")
	@GetMapping("/list")
	public TableDataInfo<JobVo> list(@Validated JobQueryBo bo) {
		return iJobService.queryPageList(bo);
	}

	/**
	 * 查询任务主列表
	 */
	@ApiOperation("查询待审核任务主列表")
	@PreAuthorize("@ss.hasPermi('job:job:checkList')")
	@GetMapping("/checkList")
	public TableDataInfo<UnTagListVo> checkList(@Validated JobQueryBo bo) {
		return iJobService.queryUncheckPageList(bo);
	}

    /**
     * 导出任务主列表
     */
    @ApiOperation("导出任务主列表")
    @PreAuthorize("@ss.hasPermi('job:job:export')")
    @Log(title = "任务主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<JobVo> export(@Validated JobQueryBo bo) {
        List<JobVo> list = iJobService.queryList(bo);
        ExcelUtil<JobVo> util = new ExcelUtil<JobVo>(JobVo.class);
        return util.exportExcel(list, "任务主");
    }

    /**
     * 获取任务主详细信息
     */
    @ApiOperation("获取任务主详细信息")
    @PreAuthorize("@ss.hasPermi('job:job:query')")
    @GetMapping("/{id}")
    public AjaxResult<JobVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iJobService.queryById(id));
    }

    /**
     * 新增任务主
     */
    @ApiOperation("新增任务主")
    //@PreAuthorize("@ss.hasPermi('job:job:add')")
    @Log(title = "任务主", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody JobAddBo bo) {
        return toAjax(iJobService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改任务主
     */
    @ApiOperation("修改任务主")
    //@PreAuthorize("@ss.hasPermi('job:job:edit')")
    @Log(title = "任务主", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody JobEditBo bo) {
        return toAjax(iJobService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除任务主
     */
    @ApiOperation("删除任务主")
    @PreAuthorize("@ss.hasPermi('job:job:remove')")
    @Log(title = "任务主" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iJobService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

	/**
	 * 获取es所有索引
	 */
	@ApiOperation("获取es所有索引")
	@PreAuthorize("@ss.hasPermi('job:job:query')")
	@GetMapping("/getEsIndex")
	public AjaxResult getEsIndex() {
		Set<String> list = iJobService.getEsIndex();
		return AjaxResult.success(list);
	}

	/**
	 * 批量插入es
	 */
	@ApiOperation("批量插入es")
	@PreAuthorize("@ss.hasPermi('job:job:add')")
	@PostMapping("/bulkInsertEs")
	public AjaxResult bulkInsertEs(@Validated @RequestBody JobQueryBo bo){
		return iJobService.bulkInsertEs(bo);
	}

	/**
	 * 状态统计
	 */
	@ApiOperation("状态统计")
	@PreAuthorize("@ss.hasPermi('job:job:count')")
	@GetMapping("/countStatus")
	@Log(title = "任务主", businessType = BusinessType.EXPORT)
	public TableDataInfo<Map> countStatus(){
		return iJobService.countStatus();
	}
}
